package com.mrjy.maintain.bo;

import com.mrjy.common.entity.SubscriptionInfo;
import com.mrjy.common.entity.SubscriptionInfo;
import com.mrjy.maintain.dao.ISubscriptionInfoDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("subscriptionInfoBo")
public class SubscriptionInfoBo implements ISubscriptionInfoBo {
    @Resource
    ISubscriptionInfoDao subscriptionInfoDao;

    @Override
    public Object txInsert(SubscriptionInfo info) {
        Map<String, Object> map = new HashMap<String, Object>();

        List<SubscriptionInfo> subscriptionInfos = subscriptionInfoDao.queryByUserIdTeacherId(info);
        if (subscriptionInfos.size() > 0) {
            map.put("err_code", 501);
            map.put("err_msg", "已经存在");
            return map;
        }

        int row = subscriptionInfoDao.insert(info);
        //设置更新时间
        if (row > 0) {
            map.put("err_code", 200);
            map.put("err_msg", "添加成功");
        } else {
            map.put("err_code", 500);
            map.put("err_msg", "添加失败");
        }
        return map;
    }

    @Override
    public Object txDeleteByIds(int[] ids) {
        Map<String, Object> map = new HashMap<String, Object>();
        int row = subscriptionInfoDao.deleteByIds(ids);
        //设置更新时间
        if (row > 0) {
            map.put("err_code", 200);
            map.put("err_msg", "删除成功");
        } else {
            map.put("err_code", 500);
            map.put("err_msg", "删除失败");
        }
        return map;
    }

    @Override
    public Object txUpdateById(SubscriptionInfo info) {
        Map<String, Object> map = new HashMap<String, Object>();
        int row = subscriptionInfoDao.updateById(info);
        //设置更新时间
        if (row > 0) {
            map.put("err_code", 200);
            map.put("err_msg", "更新成功");
        } else {
            map.put("err_code", 500);
            map.put("err_msg", "更新失败");
        }
        return map;
    }


    @Override
    public Object txUpdateDisableFlag(SubscriptionInfo info) {
        Map<String, Object> map = new HashMap<String, Object>();
        int row = subscriptionInfoDao.updateDisableFlag(info);
        //设置更新时间
        if (row > 0) {
            map.put("err_code", 200);
            map.put("err_msg", "更新成功");
        } else {
            map.put("err_code", 500);
            map.put("err_msg", "更新失败");
        }
        return map;
    }

    @Override
    public Object queryByUserId(SubscriptionInfo info) {
        Map<String, Object> map = new HashMap<String, Object>();
        SubscriptionInfo subscriptionInfo = subscriptionInfoDao.queryByUserId(info);
        //设置更新时间
        if (subscriptionInfo != null) {
            map.put("err_code", 200);
            map.put("err_msg", "查询成功");
            map.put("info", subscriptionInfo);
        } else {
            map.put("err_code", 500);
            map.put("err_msg", "查询失败");
        }
        return map;
    }

    @Override
    public Object queryByUserIdTeacherId(SubscriptionInfo info) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (info.getTeacherId() == null || info.getUserId() == null) {
            map.put("err_code", 500);
            map.put("err_msg", "参数不对");
            return map;
        }

        List<SubscriptionInfo> subscriptionInfo = subscriptionInfoDao.queryByUserIdTeacherId(info);
        //设置更新时间
        if (subscriptionInfo != null) {
            map.put("err_code", 200);
            map.put("err_msg", "查询成功");
            map.put("info", subscriptionInfo);
        } else {
            map.put("err_code", 500);
            map.put("err_msg", "查询失败");
        }
        return map;
    }

    @Override
    public Object isSubscript(SubscriptionInfo info) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (info.getTeacherId() == null && info.getUserId() == null) {
            map.put("err_code", 500);
            map.put("err_msg", "参数不对");
            return map;
        }

        List<SubscriptionInfo> subscriptionInfos = subscriptionInfoDao.isSubscript(info);

        //设置更新时间
        if (subscriptionInfos != null && subscriptionInfos.size() > 0) {
            map.put("err_code", 200);
            map.put("err_msg", "查询成功");
            map.put("info", subscriptionInfos.get(0));
        } else {
            map.put("err_code", 500);
            map.put("err_msg", "查询失败");
        }
        return map;
    }

    @Override
    public Object updateSub(SubscriptionInfo info) {
        Map<String, Object> map = new HashMap<String, Object>();

        List<SubscriptionInfo> subscriptionInfos = subscriptionInfoDao.queryByUserIdTeacherId(info);
        if (subscriptionInfos != null && subscriptionInfos.size() > 0) {
            SubscriptionInfo subscriptionInfo = subscriptionInfos.get(0);
            info.setId(subscriptionInfo.getId());
            int flag = subscriptionInfo.getDisableFlag();
            flag = flag == 0 ? 1 : 0;
            info.setDisableFlag(flag);
            int row = subscriptionInfoDao.updateById(info);
            if (row > 0) {
                map.put("err_code", 200);
                map.put("err_msg", "更新成功");
                return map;
            } else {
                map.put("err_code", 500);
                map.put("err_msg", "更新失败");
                return map;
            }
        } else {
            //数据库为空
            int row = subscriptionInfoDao.insert(info);
            if (row > 0) {
                map.put("err_code", 200);
                map.put("err_msg", "插入成功");
                return map;
            } else {
                map.put("err_code", 500);
                map.put("err_msg", "插入失败");
                return map;
            }
        }
    }
}
